package com.wenhai.spring.aop.pointcut;

import com.wenhai.spring.aop.Singe;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

/**
 * 基于{@link StaticMethodMatcherPointcut} 扩展拦截 {@link GoodGuitarist#sing(String)}
 *
 * @author wenhai
 * @date   2021/5/5
 * @see StaticMethodMatcherPointcut
 */
public class StaticPointcutDemo {
    public static void main(String[] args) {
        GoodGuitarist goodGuitarist = new GoodGuitarist();
        GreatGuitarist greatGuitarist = new GreatGuitarist();
        Singe proxyOne;
        Singe proxyTwo;
        Pointcut pc = new SimpleStaticPointcut();
        Advice advice = new SimpleAdvice();
        Advisor advisor = new DefaultPointcutAdvisor(pc, advice);
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(advisor);
        proxyFactory.setTarget(goodGuitarist);
        proxyOne = (Singe) proxyFactory.getProxy();
        proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(greatGuitarist);
        proxyTwo = (Singe) proxyFactory.getProxy();
        proxyOne.sing("文海");
        proxyTwo.sing("文海");
    }
}
